package com.xiaowenxiong.service;

import com.xiaowenxiong.dao.StudentMapper;
import com.xiaowenxiong.entity.Pager;
import com.xiaowenxiong.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * Created by 15c on 2016/11/10.
 */
@Service("Student")

public class StudentServiceImpl implements StudentService {
    @Autowired
    StudentMapper studentMapper;

    @Override
    public Student selectAstudentInfo(String sno) {
        return studentMapper.selectByPrimaryKey(sno);
    }

    @Override
    public List<Student> findAllStudent() {
        return studentMapper.selectAllStudent();
    }

    @Override
    public int addStudent(Student student) {
        return studentMapper.insertSelective(student);
    }

    @Override
    public Pager<Student> find(int pageOffset) {
        //一页多少条数据
        int pageSize = 12;
        Pager<Student> pager = new Pager<Student>();
        pager.setPageSize(pageSize);
        //一共有多少条数据
        int totalRecord = studentMapper.studentTotal();
        pager.setTotalRecord(totalRecord);
        pager.setDatas(studentMapper.selectStudentByPage(pageOffset, pageSize));
        //判断多少共有多少页
        int resultPage = totalRecord / pageSize;
        //判断是否存在余数；若存在将多一页
        int resultRemainder = totalRecord % pageSize;
        if (0 < resultRemainder) {
            resultPage = resultPage + 1;
            pager.setTotalPage(resultPage);
        } else {
            pager.setTotalPage(resultPage);
        }

        return pager;
    }

    /**
     * 用于关键字筛选分页显示
     *
     * @param pageOffset
     * @param keyword
     * @return
     */
    @Override
    public Pager<Student> findStudnetByRetrieval(int pageOffset, String keyword) {
        //一页多少条数据
        int pageSize = 12;
        String keyword1 = "%" + keyword + "%";
        Pager<Student> pager = new Pager<Student>();
        pager.setPageSize(pageSize);
        //一共有多少条数据
        int totalRecord = studentMapper.selectStudentByRetrievalTotal(keyword1, keyword1, keyword1, keyword1);
        pager.setTotalRecord(totalRecord);
        pager.setDatas(studentMapper.selectStudentByRetrieval(pageOffset, pageSize, keyword1, keyword1,keyword1, keyword1));
        //判断多少共有多少页
        int resultPage = totalRecord / pageSize;
        //判断是否存在余数；若存在将多一页
        int resultRemainder = totalRecord % pageSize;
        if (0 < resultRemainder) {
            resultPage = resultPage + 1;
            pager.setTotalPage(resultPage);
        } else {
            pager.setTotalPage(resultPage);
        }
        return pager;
    }

    /**
     * 删除学生
     *
     * @param sno 学号
     * @return 0或1
     */
    @Override
    public int deleteOneStudent(String sno) {
        return studentMapper.deleteByPrimaryKey(sno);
    }

    /**
     * 更新学生信息
     *
     * @param student 对象
     * @return 0或1
     */
    @Override
    public int updateStudent(Student student) {
        return studentMapper.updateByPrimaryKeySelective(student);
    }

    /**
     * 查询成绩
     *
     * @param sno 学号
     * @return
     */
    @Override
    public List<Student> selectGrade(String sno) {
        return studentMapper.selectStudentGrade(sno);
    }

    @Override
    public Student selectInfoStudentAndCourseAndSc(String sno, Integer cno) {
        return studentMapper.selectUpdateGradeStudentInfo(sno, cno);
    }

    /**
     * 查询总条数
     * 没使用
     *
     * @return 数据库中一共有多少条数据
     */
    @Override
    public int selectStudentTotal() {
        return studentMapper.studentTotal();
    }

    /**
     * 分页查询
     * 没使用
     *
     * @param startPos 开始位置
     * @param pageSize 一次查询多少条
     * @return 返回list数据
     */
    @Override
    public List<Student> selectStudent(int startPos, int pageSize) {
        return studentMapper.selectStudentByPage(startPos, pageSize);
    }
}
